package com.lb.uts.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * HTTP请求对象 HttpServeltRequest 的处理工具类
 */
public class RequestUtils {

    /**
     * 取得请求来源的IP地址
     * @param request
     * @return
     */
    public static String getRemoteIP(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(StringUtils.length(ip) == 0 || StringUtils.equalsIgnoreCase(ip, "unknown")){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(StringUtils.length(ip) == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(StringUtils.length(ip) == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }
}